package com.example.basketVoice

import SpeechRecognitionScreen
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import com.example.basketVoice.ui.theme.BasketVoiceTheme
import org.vosk.Model
import org.vosk.Recognizer
import com.example.basketVoice.SpeechService
import java.io.File
import java.io.IOException



class MainActivity : ComponentActivity() {
    private lateinit var model: Model
    private lateinit var recognizer: Recognizer
    private val sampleRate = 16000 // 定义采样率常量

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 初始化Vosk模型
        val modelPath = File(filesDir, "model").absolutePath
        try {
            // 确保模型目录存在
           // if (!File(modelPath).exists()) {
                FileUtils.copyAssets(this, "model/vosk-model-small-cn-0.22", modelPath)
          //  }

            model = Model(modelPath)
            recognizer = Recognizer(model, sampleRate.toFloat()) // 使用定义的采样率

            setContent {
                BasketVoiceTheme {
                    val context = LocalContext.current
                    val speechService = remember { SpeechService(recognizer, sampleRate) }

                    SpeechRecognitionScreen(
                        speechService = speechService,
                        context = context
                    )
                }
            }

        } catch (e: IOException) {
            Log.e("Vosk", "Model initialization failed", e)
            // 处理初始化失败情况
            finish()
        }
    }
}

//@Preview(showBackground = true)
//@Composable
//fun GreetingPreview() {
//    BasketVoiceTheme {
//        // 预览模式使用模拟数据
//        SpeechRecognitionScreen(
//            speechService = remember { SpeechService.mock() },
//            context = LocalContext.current
//        )
//    }
//}